package com.example.evaluate.controller;

import com.alibaba.fastjson.JSON;
import com.example.evaluate.model.CallBackMsg;
import com.example.evaluate.model.Question;
import com.example.evaluate.model.Student;
import com.example.evaluate.model.Teacher;
import com.example.evaluate.service.StudentService;
import com.example.evaluate.service.TeacherService;
import com.example.evaluate.util.AjaxUtil;
import com.example.evaluate.util.CalculateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    TeacherService teacherService;

    /*
     * 处理学生的登陆
     * */
    @RequestMapping("studentLogin")
    public void updateScenic(HttpServletResponse response, HttpServletRequest request){

        boolean flag = false;

        String studentId = request.getParameter("studentId");
        String studentPassword = request.getParameter("studentPassword");

        System.out.println(studentId+studentPassword);

        Student student = new Student(Integer.parseInt(studentId),studentPassword);

        if (studentService.login(student))
            flag = true;

        CallBackMsg msg = new CallBackMsg(flag?"success":"fail");
        System.out.println(msg.toString());

        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }

    /**
     * 获取未评教老师列表
     * */
    @RequestMapping("getTeacher")
    public void getTeacher(HttpServletResponse response, HttpServletRequest request) {
        String studentId = request.getParameter("studentId");
        //通过学生的id获取老师列表
        ArrayList<Teacher> teachers = (ArrayList<Teacher>) studentService.findTeacherByStudentId(new Student(Integer.parseInt(studentId)));

        System.out.println(teachers.toString());

        AjaxUtil.writeJSON2Response(teachers.toString(),response);
    }

    /**
     * 获取已评教老师列表
     * */
    @RequestMapping("getTeacher2")
    public void getTeacher2(HttpServletResponse response, HttpServletRequest request) {
        String studentId = request.getParameter("studentId");
        //通过学生的id获取老师列表
        ArrayList<Teacher> teachers = (ArrayList<Teacher>) studentService.findTeacherByStudentId2(new Student(Integer.parseInt(studentId)));
        System.out.println(teachers.toString());
        AjaxUtil.writeJSON2Response(teachers.toString(),response);
    }


    /**
     * 模糊查询获取老师列表
     * */
    @RequestMapping("getTeacherByParam")
    public void getTeacherByParam(HttpServletResponse response, HttpServletRequest request) {
        String studentId = request.getParameter("studentId");
        String param = request.getParameter("message");

        System.out.println(studentId+param);


        //通过学生的id获取老师列表
        ArrayList<Teacher> teachers = (ArrayList<Teacher>) studentService.findTeacherByParam(Integer.parseInt(studentId),param);

        AjaxUtil.writeJSON2Response(teachers.toString(),response);
    }

    /**
     * 通过学生id查找到学生信息
     * */
    @RequestMapping("findStudentByStudentId")
    public void findStudentByStudentId(HttpServletResponse response, HttpServletRequest request){
        String studentId = request.getParameter("studentId");

        Student student = studentService.findStudentByStudentId(new Student(Integer.parseInt(studentId)));

        AjaxUtil.writeJSON2Response(student.toString(),response);
    }




    /*
    * 修改密码
    *
    * */
    @RequestMapping("updateStudentPassword")
    public void updateStudentPassword(HttpServletResponse response, HttpServletRequest request){
        String studentId = request.getParameter("studentId");
        String studentPassword = request.getParameter("studentPassword");

        System.out.println(studentId+studentPassword);
        boolean flag = studentService.updatePassword(new Student(Integer.parseInt(studentId),studentPassword));

        CallBackMsg msg = new CallBackMsg(flag?"success":"fail");
        System.out.println(msg.toString());

        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }


    /*
     * 学生提交评价
     *
     * 1.分析入库问题答案序列
     * 2.更新老师分数以及是否评价这一项
     *
     * */
    @RequestMapping("addQuestion")
    public void addQuestion(HttpServletResponse response, HttpServletRequest request){
        String teacherId = request.getParameter("teacherId");
        String result = request.getParameter("result");
        String studentId = request.getParameter("studentId");

        String questionAdvice = request.getParameter("questionAdvice");

        //是否已经评教
        String teacherEvaluateFlag = request.getParameter("teacherEvaluateFlag");

        List<String> list = JSON.parseArray(result,String.class);

//        System.out.println(list.toString()+":"+teacherId);
//
//        for (int i = 0; i < list.size(); i++) {
//            System.out.println(i+":"+list.get(i)+" ");
//        }
//
        Question question = new Question(Integer.parseInt(list.get(0)),
                Integer.parseInt(list.get(1)),
                Integer.parseInt(list.get(2)),
                Integer.parseInt(list.get(3)),
                Integer.parseInt(list.get(4)),
                Integer.parseInt(list.get(5)),
                Integer.parseInt(list.get(6)),
                Integer.parseInt(list.get(7)),
                Integer.parseInt(list.get(8)),
                Integer.parseInt(list.get(9)),
                questionAdvice);

        //更新老师的基本信息
        Teacher teacher = new Teacher(Integer.parseInt(teacherId),1, CalculateUtil.getTotalPoint(question));
        boolean flag1 = teacherService.updateTeacherInformation(teacher);
//        System.out.println(teacherId+question.toString());

        boolean flag2 = false;

        if (teacherEvaluateFlag.equals("1")){
            //如果已经评教 要做的是修改问题
            Question tempQuestion = teacherService.findQuestionByTeacherId(Integer.parseInt(teacherId));

            System.out.println(tempQuestion.toString());

            question.setQuestionId(tempQuestion.getQuestionId());
            flag2 = teacherService.updateQuestion(question);


        }else {
            //要做添加
            flag2 = studentService.studentSubmitEvaluation(Integer.parseInt(studentId),Integer.parseInt(teacherId),question);
        }


        CallBackMsg msg = new CallBackMsg(flag1&&flag2?"success":"fail");

        System.out.println(msg.toString());

        AjaxUtil.writeJSON2Response(msg.toString(),response);
    }

    /**
     * 获取已经评教的信息
     * */
    @RequestMapping("getEvaluateQuestion")
    public void getEvaluateQuestion(HttpServletResponse response, HttpServletRequest request){
        String teacherId = request.getParameter("teacherId");

        Question question = teacherService.findQuestionByTeacherId(Integer.parseInt(teacherId));

        AjaxUtil.writeJSON2Response(question.toString(),response);
    }




}

